import { notification } from "antd";

class UserSession {
	// 设置当前登录用户的token
	static setToken(token) {
		if (token) {
			window.localStorage.setItem("robot_token", token);
		} else {
			window.localStorage.removeItem("robot_token");
		}
	}

	// 获取当前登录用户的token
	static getToken() {
		return window.localStorage.getItem("robot_token")
			? window.localStorage.getItem("robot_token")
			: "";
	}

	// 获取当前登录用户
	static getCurrentUser() {
		return window.localStorage.getItem("currentUser")
			? JSON.parse(window.localStorage.getItem("currentUser"))
			: "";
	}

	// 获取当前浏览的页面
	static getCurrentLocation() {
		return window.location.href;
	}

	// 判断当前访问的是不是系统知识库
	static isSystemKnowledge() {
		const path = this.getCurrentLocation();
		return path.indexOf("/systemKnowledge") > -1;
	}

	// 判断是否可以删除，新增等操作
	static whetherCanDelete() {
		return !this.isAdmin() && this.isSystemKnowledge();
	}

	// 判断是不是总库人员
	static isAdmin() {
		const currentUser = UserSession.getCurrentUser();
		if (currentUser) {
			return currentUser.tenantId === "2";
		}
		return false;
	}

	// 判断当前访问的是不是 总库数据
	static isSystemDataKnowledge() {
		return this.isAdmin() || this.isSystemKnowledge();
	}

	static setSystemMenu(systemMenu) {
		window.localStorage.setItem("systemMenu", systemMenu);
	}

	static getSystemMenu() {
		return window.localStorage.getItem("systemMenu")
			? JSON.parse(window.localStorage.getItem("systemMenu"))
			: null;
	}

	static setActiveMenu(activeMenu) {
		window.localStorage.setItem("activeMenu", activeMenu);
	}

	static getActiveMenu() {
		return window.localStorage.getItem("activeMenu");
	}

	static getMenuOfActiveMenu() {
		const systemMenu = this.getSystemMenu();
		const activeMenu = this.getActiveMenu();
		if (systemMenu && activeMenu) {
			return systemMenu[activeMenu];
		}
		return null;
	}

	// 账户是否到期
	static getAccountNonExpired() {
		const currentUser = UserSession.getCurrentUser();
		if (currentUser) {
			return currentUser.accountNonExpired;
		}
		return false;
	}

	// 清理缓存
	static clearCache() {
		window.localStorage.clear();
	}

	// 判断 artificialCenter 是否有权限
	static hasArtificialCenter() {
		const systemMenu = this.getSystemMenu();
		if (systemMenu) {
			return systemMenu.artificialCenter && systemMenu.artificialCenter.length > 0;
		}
		return false;
	}

	//判断是否有跳转菜单权限（是否审查，是否到期）
	static canJumpToOtherMenu() {
		const response = this.getCurrentUser()
		if (response.reviewStatus == 1 || response.reviewStatus == 2) {
			notification.info({
				message: <div>
					<div>正在审查中，如需了解进度，请联系管理员。</div>
					{/* <img src="/wxCode.png" style={{width:150,height:150}} />*/}
				</div>,
				key: 'notice'
			})
			return false
		}
		if (response.overdue) {
			notification.info({
				message: <div>
					<div>使用期限已过，请联系管理员。</div>
					{/* <img src="/wxCode.png" style={{width:150,height:150}} />*/}
				</div>,
				key: 'notice'
			})
			return false
		}

		return true
	}

	// 获取当前平台信息
	static getCurrentPlatform() {
		return window.localStorage.getItem("platformInfo")
			? JSON.parse(window.localStorage.getItem("platformInfo"))
			: {};
	}

	//判断是否有IM
	static hasIM() {
		const response = this.getCurrentPlatform()
		const res = this.getCurrentUser()
		return response.moduleKefu == 1 && res.sysPlatformSetting && res.sysPlatformSetting.moduleKefu == 1 ? true : false
	}

	//判断是否有工单
	static hasTicket() {
		const response = this.getCurrentPlatform()
		const res = this.getCurrentUser()
		return response.moduleTicket == 1 && res.sysPlatformSetting && res.sysPlatformSetting.moduleTicket == 1 ? true : false
	}

	//判断是否有数字人
	static hasDigitalPerson() {
		const response = this.getCurrentPlatform()
		const res = this.getCurrentUser()
		return response.digitalPerson == 1 && res.sysPlatformSetting && res.sysPlatformSetting.digitalPerson == 1 ? true : false
	}

	//判断是否有知识导图作用域
	static hasAtlasKnowledge() {
		const response = this.getCurrentPlatform()
		const res = this.getCurrentUser()
		return response.atlasKnowledge == 1 && res.sysPlatformSetting && res.sysPlatformSetting.atlasKnowledge == 1 ? true : false
	}

	//判断是否有多轮对话作用域
	static hasDialogKnowledge() {
		const response = this.getCurrentPlatform()
		const res = this.getCurrentUser()
		return response.dialogKnowledge == 1 && res.sysPlatformSetting && res.sysPlatformSetting.dialogKnowledge == 1 ? true : false
	}

	//判断是否有文档知识点作用域
	static hasDocKnowledge() {
		const response = this.getCurrentPlatform()
		const res = this.getCurrentUser()
		return response.docKnowledge == 1 && res.sysPlatformSetting && res.sysPlatformSetting.docKnowledge == 1 ? true : false
	}

  //判断是否有大语言模型
	static hasLargeLanguage() {
		const response = this.getCurrentPlatform()
		const res = this.getCurrentUser()
		return response.largeLanguage == 1 && res.sysPlatformSetting && res.sysPlatformSetting.largeLanguage == 1 ? true : false
	}

  //判断是否有工单
	static hasWorkOrder() {
		const res = this.getCurrentUser()    
		return res && res.menus && res.menus.workOrderCenter && res.menus.workOrderCenter.length > 0 ? true : false
	}

	//记录当前系统的左侧菜单
	static setLeftMenu(leftMenu) {
		window.localStorage.setItem("leftMenu", leftMenu);
	}

	static getLeftMenu() {
		return window.localStorage.getItem("leftMenu");
	}
}

export default UserSession;
